from flask import Blueprint, request, jsonify

bp_coupon = Blueprint("coupon", __name__)

from comm import get_data
from db import db1


@bp_coupon.route('/couponM', methods=['POST', 'GET', 'PUT', 'DELETE'])
def couponM():
    if request.method == 'POST':
        data = get_data()
        sql = "insert into coupon values(0,'%s',%d,'%s','%s')" % (
            data['name'], int(data['total']), data['stime'], data['etime'])
        db1.insert(sql)
        return jsonify({'code': 200})
    elif request.method == 'GET':
        id = request.args.get('id')
        sql = "select * from coupon where id=%d" % (int(id))
        res = db1.find(sql)
        return jsonify({'code': 200, 'list': res})
    elif request.method == 'PUT':
        data = get_data()
        sql = "update coupon set name='%s',total=%d,start_time='%s',end_time='%s' where id=%d" % (
            data['name'], int(data['total']), data['stime'], data['etime'], int(data['id']))
        db1.insert(sql)
        return jsonify({'code': 200})
    else:
        id = request.args.get('id')
        sql = "delete from coupon where id=%d" % (int(id))
        db1.insert(sql)
        return jsonify({'code': 200})
